package com.staff.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.staff.entity.Staff;
import com.staff.entity.StaffContract;

public class StaffContractQueryDTO {
	private Long id;
	private String contractNum;
	private String contractType;
	private Date contractStarDate;
	private Date contractEndDate;
	private String position;
	private String contractContent;
	private String staffName;
	private String staffNum;
	private Long staffId;
	
	public StaffContractQueryDTO(StaffContract sc,Staff staff) {
		super();
		this.id = sc.getId();
		this.contractNum = sc.getContractNum();
		this.contractType = sc.getContractType();
		this.contractStarDate = sc.getContractStarDate();
		this.contractEndDate = sc.getContractEndDate();
		this.position = sc.getPosition();
		this.contractContent = sc.getContractContent();
		this.staffName = staff.getStaffName();
		this.staffNum = staff.getStaffNum();
		this.staffId = staff.getId();
	}
	
	public String getStaffNum() {
		return staffNum;
	}
	public void setStaffNum(String staffNum) {
		this.staffNum = staffNum;
	}

	public Long getStaffId() {
		return staffId;
	}

	public void setStaffId(Long staffId) {
		this.staffId = staffId;
	}

	public StaffContractQueryDTO() {
		super();
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getContractNum() {
		return contractNum;
	}
	public void setContractNum(String contractNum) {
		this.contractNum = contractNum;
	}
	public String getContractType() {
		return contractType;
	}
	public void setContractType(String contractType) {
		this.contractType = contractType;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getContractStarDate() {
		return contractStarDate;
	}
	public void setContractStarDate(Date contractStarDate) {
		this.contractStarDate = contractStarDate;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getContractEndDate() {
		return contractEndDate;
	}
	public void setContractEndDate(Date contractEndDate) {
		this.contractEndDate = contractEndDate;
	}
	public String getPosition() {
		return position;
	}
	public void setPosition(String position) {
		this.position = position;
	}
	public String getContractContent() {
		return contractContent;
	}
	public void setContractContent(String contractContent) {
		this.contractContent = contractContent;
	}
	public String getStaffName() {
		return staffName;
	}
	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}
	//提供static的工具方法：根据当前 userQueryDTO 对象来组装动态查询条件
	public static Specification<StaffContract> getSpecification(StaffContractQueryDTO userQueryDTO)
	{
		Specification<StaffContract> spec = new Specification<StaffContract>() {
			public Predicate toPredicate(Root<StaffContract> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != userQueryDTO && !StringUtils.isEmpty(userQueryDTO.getStaffName())) {
					 Predicate  p1 =  cb.like(root.get("staffName").as(String.class),"%"+ userQueryDTO.getStaffName() + "%");
					 list.add(p1);
				 }
				 if(null != userQueryDTO && !StringUtils.isEmpty(userQueryDTO.getStaffNum())) {
					 Predicate  p2 =  cb.like(root.get("staffNum").as(String.class),"%"+ userQueryDTO.getStaffNum() + "%");
					 list.add(p2);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
